/* -------------------------------------------------------------------------
 *  This file is part of the Cantian project.
 * Copyright (c) 2023 Huawei Technologies Co.,Ltd.
 *
 * Cantian is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *          http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * -------------------------------------------------------------------------
 *
 * tse_ddl.h
 *
 *
 * IDENTIFICATION
 * src/tse/tse_ddl.h
 *
 * -------------------------------------------------------------------------
 */
#ifndef __TSE_DDL_H__
#define __TSE_DDL_H__

#include "cm_defs.h"
#include "srv_session.h"
#include "srv_query.h"
#include "tse_srv.h"
#include "protobuf/tc_db.pb-c.h"

#define DATA_FILE_DEFALUT_EXTEND_SIZE SIZE_M(16)
#define PARTITION_MIN_CNT 2
#define TABLESPACE_PATH_MAX_LEN 256
#define EMPTY_DATABASE ""
#define INVALID_PART_ID (uint32)0xFFFFFFFF
#define TSE_DROP_NO_CHECK_FK_FOR_CANTIAN_AND_BROADCAST 0x01000000 // value need equal to "ha_tse_ddl.h" file defined

#define ALTER_TABLE_DDL_LOCKED 2

#define FILL_BROADCAST_REQ(broadcast_req, _db_name, _sql_str, _user_name, _user_ip, _mysql_inst_id, _sql_command) \
    do {                                                                                                          \
        (broadcast_req).mysql_inst_id = (_mysql_inst_id);                                                         \
        (broadcast_req).sql_command = (_sql_command);                                                             \
        int cpy_ret = strcpy_s((broadcast_req).sql_str, MAX_DDL_SQL_LEN, (_sql_str));                             \
        knl_securec_check(cpy_ret);                                                                               \
        if (!CM_IS_EMPTY_STR((char *)(_db_name))) {                                                               \
            cpy_ret = strcpy_s((broadcast_req).db_name, SMALL_RECORD_SIZE, (_db_name));                           \
            knl_securec_check(cpy_ret);                                                                           \
        }                                                                                                         \
        cpy_ret = strcpy_s((broadcast_req).user_name, SMALL_RECORD_SIZE, (_user_name));                           \
        knl_securec_check(cpy_ret);                                                                               \
        cpy_ret = strcpy_s((broadcast_req).user_ip, SMALL_RECORD_SIZE, (_user_ip));                               \
        knl_securec_check(cpy_ret);                                                                               \
        (broadcast_req).options = 0;                                                                              \
    } while (0)

typedef enum sql_command_filter_op {
    SQLCOM_CREATE_INDEX = 2,
    SQLCOM_ALTER_TABLE = 3,
    SQLCOM_DROP_TABLE = 9,
    SQLCOM_DROP_INDEX = 10,
    SQLCOM_SET_OPTION = 31,
    SQLCOM_CREATE_DB = 36,
    SQLCOM_RENAME_TABLE = 64,
    SQLCOM_LOCK_INSTANCE = 154,
    SQLCOM_UNLOCK_INSTANCE = 155,
} sql_command_filter_op_t;

void unlock_user_ddl(session_t *session);
int tse_close_mysql_connection(tianchi_handler_t *tch);
gs_type_t get_gs_type_from_tse_ddl_type(enum_tse_ddl_field_types tse_type);
void tse_ddl_clear_stmt(sql_stmt_t *stmt);
status_t tse_sql_parse_hash_partition(sql_stmt_t *stmt, knl_part_def_t *part_def);
status_t tse_sql_subpart_parse_partition(sql_stmt_t *stmt, knl_part_def_t *part_def, knl_part_obj_def_t *obj_def);
int fill_partition_column_info(text_t *column_name, knl_part_column_def_t *part_column, knl_table_def_t *def);
int fill_sub_partition_column_info(TcDb__TseDDLCreateTableDef *req, knl_part_obj_def_t *obj_def, knl_table_def_t *def);
int fill_alter_part_key(knl_dictionary_t dc, knl_altable_def_t *def, knl_column_t *knl_column);
int fill_alter_subpart_key(knl_dictionary_t dc, knl_altable_def_t *def, knl_column_t *knl_column);
void tse_alter_table_unlock(knl_session_t *knl_session);
#endif //__TSE_DDL_H__
